package abio.XasML.rpc
{
	import abio.XasML.util.tween;
	
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;
	import flash.utils.Proxy;
	import flash.utils.flash_proxy;
	
	import mx.core.mx_internal;
	import mx.messaging.messages.AbstractMessage;
	import mx.rpc.AsyncToken;
	import mx.rpc.events.ResultEvent;

	public class staticLoader extends Proxy
	{
		static private const urlLoaderDict:Dictionary=new Dictionary();
		private var url:String=null;

		public function staticLoader(url:String)
		{
			this.url=url;
		}
		
		override flash_proxy function getProperty(name:*):*
		{
			if(staticLoader.urlLoaderDict.hasOwnProperty(url+name)==false){
				staticLoader.urlLoaderDict[url+name]=new URLLoader();
				(staticLoader.urlLoaderDict[url+name] as URLLoader).dataFormat=URLLoaderDataFormat.BINARY;
				(staticLoader.urlLoaderDict[url+name] as URLLoader).load(new URLRequest(url+name));
			}
			return function delayedCall():AsyncToken{
				var result:AsyncToken=new AsyncToken(new AbstractMessage());
				if((staticLoader.urlLoaderDict[url+name] as URLLoader).data==undefined){
					(staticLoader.urlLoaderDict[url+name] as URLLoader).addEventListener(Event.COMPLETE, function COMPLETE(e:Event):void{
						result.mx_internal::applyResult(new ResultEvent(typeof((staticLoader.urlLoaderDict[url+name] as URLLoader).data),false,true,(staticLoader.urlLoaderDict[url+name] as URLLoader).data));
					});
				}else{
					tween.delayedCall(1, function DELAYEDCALL():void{
						result.mx_internal::applyResult(new ResultEvent(typeof((staticLoader.urlLoaderDict[url+name] as URLLoader).data),false,true,(staticLoader.urlLoaderDict[url+name] as URLLoader).data));
					});
				}
				return result;
			}
		}
	}
}
